package com.example.jpademo.common.mysql;

import com.mysql.cj.jdbc.JdbcStatement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class BatchInsertData {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void prepare(String loadSql, InputStream is) throws SQLException {
        Connection conn = jdbcTemplate.getDataSource().getConnection();
        PreparedStatement psmt = conn.prepareStatement(loadSql);
        int result = 0;
        if(psmt.isWrapperFor(JdbcStatement.class)){
            JdbcStatement statement = psmt.unwrap(JdbcStatement.class);
            statement.setLocalInfileInputStream(is);
        }
        result = psmt.executeUpdate();
    }

    public static void main(String[] args) {
        String sql = "load data local infile 'sql.csv' ingore into table tmp01(name)";
    }
}
